package com.shop.service.impl;

import com.shop.entity.SysRole;
import com.shop.entity.SysRolePerssion;
import com.shop.entity.SysUser;
import com.shop.entity.SysUserRole;
import com.shop.mapper.SysRoleMapper;
import com.shop.mapper.SysRolePerssionMapper;
import com.shop.service.SysRolePerssionService;
import com.shop.service.SysRoleService;
import com.shop.utils.PageUtils;
import com.shop.utils.ResultUtils;
import com.shop.utils.ShiroPasswordUtils;
import com.shop.utils.StringEmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl  implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    SysRolePerssionMapper sysRolePerssionMapper;

    @Override
    public List<SysRole> findAllRole() {
        return sysRoleMapper.findAllRole();
    }

    @Override
    @Transactional
    public ResultUtils insertRolePerssion(SysRole record,List<Integer> perssionId) {
        if (!StringEmptyUtils.isEmpty(record.getRoleName())){
            return ResultUtils.error(500,"角色名称不能为空");
        }
        record.setCreateDate(new Date());
        record.setUpdateDate(new Date());
        int role = sysRoleMapper.insert(record);
        if (perssionId!=null&&perssionId.size()>0) {
            perssionId.forEach(perssion->{
                SysRolePerssion sysRolePerssion=new SysRolePerssion();
                sysRolePerssion.setRoleId(record.getId());
                sysRolePerssion.setPerssionId(perssion);
                sysRolePerssion.setCreateDate(new Date());
                sysRolePerssion.setUpdateDate(new Date());
                sysRolePerssionMapper.insert(sysRolePerssion);
            });
        }
        if (role>0) {
            return  ResultUtils.ok();
        }
        return ResultUtils.error(500,"新增失败");
    }

    @Override
    public ResultUtils deleteRoleSysPerssion(Integer roleId) {
        int role = sysRoleMapper.deleteByPrimaryKey(roleId);
        Integer roleUserId = sysRolePerssionMapper.deleteByRoleId(roleId);
        if(role>0&&roleUserId>0){
            return  ResultUtils.ok();
        }
        return ResultUtils.error(500,"删除失败");
    }

    @Override
    @Transactional
    public ResultUtils updateRoleSysPerssion(SysRole sysRole, List<Integer> perssionId) {
        if(sysRole!=null){
            sysRole.setUpdateDate(sysRole.getUpdateDate());
            if((!StringEmptyUtils.isEmpty(sysRole.getRoleName()))&&(!StringEmptyUtils.isEmpty(sysRole.getRoleRemark()))) {
                return  ResultUtils.error(500,"请你选择要修改的数据");
            }
            if((!StringEmptyUtils.isEmpty(sysRole.getId()))){
                return  ResultUtils.error(500,"主键id不能为空");
            }
            sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        }

        List<Integer> roleIds = sysRolePerssionMapper.findRolePerssionId(sysRole.getId());
        if (roleIds.size() > 0) {
            if (perssionId!=null&&perssionId.size() > 0) {
                sysRolePerssionMapper.deleteByRoleId(sysRole.getId());
                perssionId.forEach((perssion)->{
                    SysRolePerssion  rolePerssion=new SysRolePerssion();
                    rolePerssion.setUpdateDate(new Date());
                    rolePerssion.setCreateDate(new Date());
                    rolePerssion.setPerssionId(perssion);
                    rolePerssion.setRoleId(sysRole.getId());
                    sysRolePerssionMapper.insert(rolePerssion);
                });
            }

        }
            return  ResultUtils.ok();
    }

    @Override
    public ResultUtils findRoleSysPerssion(String roleName, Integer current, Integer size) {
        Map<String,Object> pageParams=new HashMap<String,Object>();
        if (current == null) {
            current = 1;
        }
        if (size == null ) {
            size = 10;
        }
        pageParams.put("current",current);
        pageParams.put("size",size);
        pageParams.put("roleName",roleName);
        List<SysRole> userRoleByNameOrAll = sysRoleMapper.findRolePerssionOrByRoleName(pageParams);
        Integer userRoleByNameOrAllCount = sysRoleMapper.findRolePerssionOrByRoleNameCount(pageParams);
        return  PageUtils.pageInfo(userRoleByNameOrAll,userRoleByNameOrAllCount,current,size);

    }
}
